package com.attendance.www.hr.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attendance.www.hr.beans.RetroactiveApprovalBean;
import com.attendance.www.hr.beans.RetroactiveApprovalPageBean;
import com.attendance.www.hr.beans.RetroactiveApprovalParamBean;
import com.attendance.www.hr.beans.UserEmployeeBean;
import com.attendance.www.hr.constant.AppConstant;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IApprovalService;
import com.zhangkongli.frame.core.ResultMap;
/**
 * 补签审批功能
 * @version 2017/10/02
 * @author 万波
 *
 */
@Controller
@RequestMapping("/retroactiveApproval")
public class RetroactiveApprovalController {

	@Resource
	private IApprovalService approvalService;

	/**
	 * 获取待审批补签记录
	 * @param request
	 * @param attendanceBean
	 * @return
	 */
	@RequestMapping(value = "/getRetroactiveApprovalList.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getRetroactiveApprovalList(HttpServletRequest request,@RequestBody RetroactiveApprovalParamBean paramBean) {
		//取得登录用户信息
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}

		Integer pageNo = paramBean.getPageNo();
		Integer pageCount = paramBean.getPageCount();
		
		if (pageNo ==null || pageCount==null) {
			throw new ResultFaultException("0_00001");
		}
		
		Integer startNum = 0;
		if (pageNo > 1) {
			startNum = (pageNo - 1) * pageCount;
		}
		paramBean.setStartNum(startNum);
		
		//获取补签列表
		RetroactiveApprovalPageBean retroactiveApprovalPageBean = approvalService.getRetroactiveApprovalList(paramBean,userEmployee.getEmployeeId());
	
		return ResultMap.success(retroactiveApprovalPageBean);
	}
	/**
	 * 获取待审批补签记录
	 * @param request
	 * @param attendanceBean
	 * @return
	 */
	@RequestMapping(value = "/getRetroactiveApprovalListByRole.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getRetroactiveApprovalListByRole(HttpServletRequest request,@RequestBody RetroactiveApprovalParamBean paramBean) {
		//取得登录用户信息
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}

		Integer pageNo = paramBean.getPageNo();
		Integer pageCount = paramBean.getPageCount();
		
		if (pageNo ==null || pageCount==null) {
			throw new ResultFaultException("0_00001");
		}
		
		Integer startNum = 0;
		if (pageNo > 1) {
			startNum = (pageNo - 1) * pageCount;
		}
		paramBean.setStartNum(startNum);
		
		//获取补签列表
		RetroactiveApprovalPageBean retroactiveApprovalPageBean = approvalService.getRetroactiveApprovalListByRole(paramBean,userEmployee.getEmployeeId());
	
		return ResultMap.success(retroactiveApprovalPageBean);
	}
	
	/**
	 * 审核操作	
	 * @param request
	 * @param RetroactiveApprovalBean
	 * @return
	 */
	@RequestMapping(value = "/saveRetroactiveApproval.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object saveRetroactiveApproval(HttpServletRequest request,@RequestBody RetroactiveApprovalBean retroactiveApprovalBean) {
		//取得登录用户自己岗位信息
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		
		//验证必填项
		if (retroactiveApprovalBean.getApprovalState()==null 
				|| retroactiveApprovalBean.getRetroactiveIdList()==null 
				|| retroactiveApprovalBean.getRetroactiveIdList().size()<=0) {
			throw new ResultFaultException("0_00001");
		}
		
		if(approvalService.saveRetroactiveApproval(retroactiveApprovalBean,userEmployee.getEmployeeId())){
			return ResultMap.success();
		}else{
			throw new ResultFaultException("2_00002");
		}
	}
	
	@RequestMapping(value = "/getApprovalRecordById.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object saveRetroactiveApproval(HttpServletRequest request) {
		String retroactiveIdToString = request.getParameter("retroactiveId");
		if (StringUtils.isBlank(retroactiveIdToString)){
			throw new ResultFaultException("0_00001");
		}
		Integer retroactiveId = Integer.valueOf(retroactiveIdToString);
		return ResultMap.success(approvalService.getApprovalRecordById(retroactiveId));
	}
	
}
